﻿/*
===========================================================================

  Copyright (c) 2010-2014 Darkstar Dev Teams

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see http://www.gnu.org/licenses/

  This file is part of DarkStar-server source code.

===========================================================================
*/

#include "../../common/showmsg.h"

#include "lua_spell.h"
#include "../utils/battleutils.h"


/************************************************************************
*																		*
*  Конструктор															*
*  Designer                                                             *
*																		*
************************************************************************/

CLuaSpell::CLuaSpell(lua_State *L)
{
	if( !lua_isnil(L,-1) )
	{
		m_PLuaSpell = (CSpell*)(lua_touserdata(L,-1));
		lua_pop(L,1);
	}
	else
	{
		m_PLuaSpell = NULL;
	}
}

/************************************************************************
*																		*
*  Конструктор															*
*  Designer                                                             *
*																		*
************************************************************************/

CLuaSpell::CLuaSpell(CSpell* PSpell)
{
	m_PLuaSpell = PSpell;
}

/************************************************************************
*                                                                       *
*  Устанавливаем сообщение заклинания                                   *
*  Set the message of the Spell                                         *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::setMsg(lua_State *L)
{
    DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
    DSP_DEBUG_BREAK_IF(lua_isnil(L,-1) || !lua_isnumber(L,-1));

    m_PLuaSpell->setMessage(lua_tointeger(L,-1));
	return 0;
}

/************************************************************************
*                                                                       *
*                                                                       *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::setAoE(lua_State *L)
{
	DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
	DSP_DEBUG_BREAK_IF(lua_isnil(L, -1) || !lua_isnumber(L, -1));

	m_PLuaSpell->setAOE(lua_tointeger(L, -1));
	return 0;
}

/************************************************************************
*                                                                       *
*                                                                       *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::setFlag(lua_State *L)
{
	DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
	DSP_DEBUG_BREAK_IF(lua_isnil(L, -1) || !lua_isnumber(L, -1));

	m_PLuaSpell->setFlag(lua_tointeger(L, -1));
	return 0;
}

/************************************************************************
*                                                                       *
*                                                                       *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::setRadius(lua_State* L)
{
	DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
	DSP_DEBUG_BREAK_IF(lua_isnil(L, -1) || !lua_isnumber(L, -1));

	m_PLuaSpell->setRadius(lua_tonumber(L, -1));
	return 0;
}

/************************************************************************
*                                                                       *
*                                                                       *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::setAnimation(lua_State* L)
{
	DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
	DSP_DEBUG_BREAK_IF(lua_isnil(L, -1) || !lua_isnumber(L, -1));

	m_PLuaSpell->setAnimationID(lua_tonumber(L, -1));
	return 0;
}

/************************************************************************
*                                                                       *
*                                                                       *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::setMPCost(lua_State* L)
{
	DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
	DSP_DEBUG_BREAK_IF(lua_isnil(L, -1) || !lua_isnumber(L, -1));

	m_PLuaSpell->setMPCost(lua_tonumber(L, -1));
	return 0;
}

/************************************************************************
*                                                                       *
*  Make sure the Spell can target the enemy.                            *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::canTargetEnemy(lua_State* L)
{
    DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
    lua_pushboolean(L, m_PLuaSpell->canTargetEnemy());
    return 1;
}

/************************************************************************
*                                                                       *
*  Return the total targets of the Spell.                               *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::getTotalTargets(lua_State* L)
{
    DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
    lua_pushinteger(L, m_PLuaSpell->getTotalTargets());
    return 1;
}

/************************************************************************
*                                                                       *
*  Return the Magic Burst message of the Spell.                         *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::getMagicBurstMessage(lua_State* L)
{
    DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
    lua_pushinteger(L, m_PLuaSpell->getMagicBurstMessage());
    return 1;
}

/************************************************************************
*                                                                       *
*  Return the Element of the Spell.                                     *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::getElement(lua_State *L)
{
    DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
	lua_pushinteger(L,m_PLuaSpell->getElement());
	return 1;
}

/************************************************************************
*                                                                       *
*  Return if the Spell is AoE.                                          *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::isAoE(lua_State *L)
{
	DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
	lua_pushinteger(L,m_PLuaSpell->getAOE());
	return 1;
}

/************************************************************************
*                                                                       *
*  Return the ID of the Spell.                                          *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::getID(lua_State *L)
{
	DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
	lua_pushinteger(L,m_PLuaSpell->getID());
	return 1;
}

/************************************************************************
*                                                                       *
*  Return the group of the Spell.                                       *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::getSpellGroup(lua_State *L)
{
	DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
	lua_pushinteger(L,m_PLuaSpell->getSpellGroup());
	return 1;
}

/************************************************************************
*                                                                       *
*                                                                       *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::getFlag(lua_State *L)
{
	DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
	lua_pushinteger(L, m_PLuaSpell->getFlag());
	return 1;
}

/************************************************************************
*                                                                       *
*  Return the Skill Type of the Spell.                                  *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::getSkillType(lua_State *L)
{
	DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
	lua_pushinteger(L,m_PLuaSpell->getSkillType());
	return 1;
}

/************************************************************************
*                                                                       *
*   Return the MP Cost of the Spell.                                    *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::getMPCost(lua_State *L)
{
    DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
    lua_pushinteger(L,m_PLuaSpell->getMPCost());
    return 1;
}

/************************************************************************
*                                                                       *
*   Return the CE Enmity of the Spell.                                  *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::getCE(lua_State *L)
{
    DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
    lua_pushinteger(L,m_PLuaSpell->getCE());
    return 1;
}

/************************************************************************
*                                                                       *
*   Return the VE Enmity of the Spell.                                  *
*                                                                       *
************************************************************************/

inline int32 CLuaSpell::getVE(lua_State *L)
{
    DSP_DEBUG_BREAK_IF(m_PLuaSpell == NULL);
    lua_pushinteger(L,m_PLuaSpell->getVE());
    return 1;
}

/************************************************************************
*																		*
*  Инициализация методов в lua											*
*  Initialization methods in lua                                        *
*																		*
************************************************************************/

const int8 CLuaSpell::className[] = "CSpell";
Lunar<CLuaSpell>::Register_t CLuaSpell::methods[] =
{
    LUNAR_DECLARE_METHOD(CLuaSpell,setMsg),
	LUNAR_DECLARE_METHOD(CLuaSpell,setAoE),
	LUNAR_DECLARE_METHOD(CLuaSpell,setFlag),
	LUNAR_DECLARE_METHOD(CLuaSpell,setRadius),
	LUNAR_DECLARE_METHOD(CLuaSpell,setAnimation),
	LUNAR_DECLARE_METHOD(CLuaSpell,setMPCost),
    LUNAR_DECLARE_METHOD(CLuaSpell,isAoE),
    LUNAR_DECLARE_METHOD(CLuaSpell,getMagicBurstMessage),
    LUNAR_DECLARE_METHOD(CLuaSpell,getElement),
	LUNAR_DECLARE_METHOD(CLuaSpell,getTotalTargets),
	LUNAR_DECLARE_METHOD(CLuaSpell,getID),
	LUNAR_DECLARE_METHOD(CLuaSpell,getSpellGroup),
	LUNAR_DECLARE_METHOD(CLuaSpell,getFlag),
	LUNAR_DECLARE_METHOD(CLuaSpell,getSkillType),
    LUNAR_DECLARE_METHOD(CLuaSpell,getMPCost),
    LUNAR_DECLARE_METHOD(CLuaSpell,getCE),
    LUNAR_DECLARE_METHOD(CLuaSpell,getVE),
	{NULL,NULL}
};